package system

import (
	"context"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/system"
	systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
)

type SysPrescriptionService struct{}

// CreateSysPrescription 创建sysPrescription表记录
// Author [yourname](https://github.com/yourname)
func (sysPrescriptionService *SysPrescriptionService) CreateSysPrescription(ctx context.Context, sysPrescription *system.SysPrescription) (err error) {
	var user system.SysUser
	err = global.GVA_DB.Where("id = ?", sysPrescription.UserId).First(&user).Error
	if err != nil {
		return err
	}
	StoreId := user.StoreId
	sysPrescription.StoresId = int(StoreId)

	err = global.GVA_DB.Create(sysPrescription).Error
	return err
}

// DeleteSysPrescription 删除sysPrescription表记录
// Author [yourname](https://github.com/yourname)
func (sysPrescriptionService *SysPrescriptionService) DeleteSysPrescription(ctx context.Context, id string) (err error) {
	err = global.GVA_DB.Delete(&system.SysPrescription{}, "id = ?", id).Error
	return err
}

// DeleteSysPrescriptionByIds 批量删除sysPrescription表记录
// Author [yourname](https://github.com/yourname)
func (sysPrescriptionService *SysPrescriptionService) DeleteSysPrescriptionByIds(ctx context.Context, ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]system.SysPrescription{}, "id in ?", ids).Error
	return err
}

// UpdateSysPrescription 更新sysPrescription表记录
// Author [yourname](https://github.com/yourname)
func (sysPrescriptionService *SysPrescriptionService) UpdateSysPrescription(ctx context.Context, sysPrescription system.SysPrescription) (err error) {
	err = global.GVA_DB.Model(&system.SysPrescription{}).Where("id = ?", sysPrescription.Id).Updates(&sysPrescription).Error
	return err
}

// GetSysPrescription 根据id获取sysPrescription表记录
// Author [yourname](https://github.com/yourname)
func (sysPrescriptionService *SysPrescriptionService) GetSysPrescription(ctx context.Context, id string) (sysPrescription system.SysPrescription, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&sysPrescription).Error
	return
}

// GetSysPrescriptionInfoList 分页获取sysPrescription表记录
// Author [yourname](https://github.com/yourname)
func (sysPrescriptionService *SysPrescriptionService) GetSysPrescriptionInfoList(ctx context.Context, info systemReq.SysPrescriptionSearch, StoreId uint) (list []system.SysPrescription, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&system.SysPrescription{})
	var sysPrescriptions []system.SysPrescription
	// 如果有条件搜索 下方会自动创建搜索语句

	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Where("stores_id = ?", StoreId).Find(&sysPrescriptions).Error
	return sysPrescriptions, total, err
}
func (sysPrescriptionService *SysPrescriptionService) GetSysPrescriptionPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
